package com.lwbpro.system.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwbpro.common.exception.ServiceException;
import com.lwbpro.common.utils.DateUtils;
import com.lwbpro.system.service.IAchievementManageService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.lwbpro.system.mapper.StudentManageMapper;
import com.lwbpro.system.domain.StudentManage;
import com.lwbpro.system.service.IStudentManageService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 学生管理Service业务层处理
 *
 * @author achievement
 * @date 2024-08-28
 */
@Service
public class StudentManageServiceImpl extends ServiceImpl<StudentManageMapper, StudentManage> implements IStudentManageService {
    @Resource
    private StudentManageMapper studentManageMapper;

    @Resource
    @Lazy
    private IAchievementManageService achievementManageService;

    /**
     * 查询学生管理
     *
     * @param id 学生管理主键
     * @return 学生管理
     */
    @Override
    public StudentManage selectStudentManageById(Long id) {
        return studentManageMapper.selectStudentManageById(id);
    }

    /**
     * 查询学生管理列表
     *
     * @param studentManage 学生管理
     * @return 学生管理
     */
    @Override
    public List<StudentManage> selectStudentManageList(StudentManage studentManage) {
        return studentManageMapper.selectStudentManageList(studentManage);
    }

    /**
     * 新增学生管理
     *
     * @param studentManage 学生管理
     * @return 结果
     */
    @Override
    public int insertStudentManage(StudentManage studentManage) {
        studentManage.setCreateTime(DateUtils.getNowDate());
        return studentManageMapper.insertStudentManage(studentManage);
    }

    /**
     * 修改学生管理
     *
     * @param studentManage 学生管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateStudentManage(StudentManage studentManage) {
        //判断名字是否存在
        StudentManage checkName = studentManageMapper.checkName(studentManage);
        if (checkName != null) {
            throw new ServiceException(studentManage.getStudentName() + "已被使用,更新失败!");
        }
        achievementManageService.updateExamManageByStudentId(studentManage.getId(), studentManage.getStudentName());
        studentManage.setUpdateTime(DateUtils.getNowDate());
        return studentManageMapper.updateStudentManage(studentManage);
    }

    /**
     * 批量删除学生管理
     *
     * @param ids 需要删除的学生管理主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteStudentManageByIds(Long[] ids) {
        achievementManageService.deleteAchievementManageByStudentIds(ids);
        return studentManageMapper.deleteStudentManageByIds(ids);
    }

    /**
     * 删除学生管理信息
     *
     * @param id 学生管理主键
     * @return 结果
     */
    @Override
    public int deleteStudentManageById(Long id) {
        return studentManageMapper.deleteStudentManageById(id);
    }

    @Override
    public List<StudentManage> selectByNames(List<String> studentNames) {
        if (CollUtil.isEmpty(studentNames)) {
            return CollUtil.newArrayList();
        }
        return studentManageMapper.selectByNames(studentNames);
    }
}
